﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nova_DAL
{
   public class MaterialType
    {
        DataSet ds = null;
        string strSQL = "";
        //string UserMenuList = "";          //用户菜单字段
        Nova_DAL.DbHelper dbHelper = new DbHelper();
        DbCommand cmd = null;
        int i = 0;
        //int strMenuID = 0;

        /// <summary>
        /// 获取物料类型数据列表
        /// </summary>
        /// <returns></returns>
        public DataSet ListMaterialType()
        {
            try
            {
                strSQL = "select TypeID,TypeName,ParentTypeID,CID,CDT from TMaterialType order by TypeID";
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                ds = dbHelper.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return ds;
        }

        /// <summary>
        /// 获取指定类别ID的类别信息
        /// </summary>
        /// <param name="TypeID"></param>
        /// <returns></returns>
        public DataSet ListMaterialType(string sTypeID)
        {
            try
            {
                strSQL = "select TypeID,TypeName,ParentTypeID,CID,CDT from TMaterialType where TypeID ='" + sTypeID + "' order by TypeID";
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                ds = dbHelper.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return ds;
        }


        /// <summary>
        /// 删除物料类别
        /// </summary>
        /// <param name="sTypeID"></param>
        /// <param name="strMessage">异常有提示，其他为“”</param>
        /// <returns>-1:删除异常  0：无此项数据  1：成功删除</returns>
        public int DelMaterialType(string sTypeID, out string strMessage)
        {
            int res = 1;
            strMessage = "";
            try
            {
                strSQL = "delete from TMaterialType where TypeID ='" + sTypeID + "' ";
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                i = dbHelper.ExecuteNonQuery(cmd);
                res = i;
            }
            catch
            {
                res = -1;
                strMessage = "删除操作异常跳出";
            }
            return res;
        }

        /// <summary>
        /// 判断类别是否是父类
        /// </summary>
        /// <param name="sTypeID"></param>
        /// <param name="strMessage"></param>
        /// <returns>1:父类 ；0：子类； -1：判断失败</returns>
        public int JudgeFatherType(string sTypeID, out string strMessage)
        {
            int flag = 0;
            strMessage = "";
            try
            {
                strSQL = "select ParentTypeID from TMaterialType where TypeID ='" + sTypeID + "' ";
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                ds = dbHelper.ExecuteDataSet(cmd);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["ParentTypeID"].ToString() == "0")
                    {
                        flag = 1;
                    }

                }
                else
                {
                    flag = -1;
                    strMessage = "没有获取到物料类别的父类类型";
                }
            }
            catch
            {
                flag = -1;
                strMessage = "判断物料类别是否父类异常跳出";
            }
            return flag;
        }

        /// <summary>
        /// 判断是否能删除（父类别）
        /// </summary>
        /// <param name="sTypeID"></param>
        /// <param name="strMessage"></param>
        /// <returns>1：能删除  0：不能删除  -1：方法异常</returns>
        public int JudageDelFatherType(string sTypeID, out string strMessage)
        {
            int flag = 0;
            strMessage = "";
            try
            {
                strSQL = "select * from TMaterialType where ParentTypeID ='" + sTypeID + "' ";
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                ds = dbHelper.ExecuteDataSet(cmd);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    flag = 0;
                    strMessage = "此类别下还关联有子类，不能删除";
                }
                else
                {
                    flag = 1;

                }
            }
            catch
            {
                flag = -1;
                strMessage = "判断父类别是否能删除方法异常跳出";
            }
            return flag;
        }

        /// <summary>
        /// 判断是否能删除（子类别）
        /// </summary>
        /// <param name="sTypeID"></param>
        /// <param name="strMessage"></param>
        /// <returns>1：能删除  0：不能删除  -1：方法异常</returns>
        public int JudageDelsubType(string sTypeID, out string strMessage)
        {
            int flag = 0;
            strMessage = "";
            try
            {
                strSQL = "select * from TBase_Material where MiniTypeID ='" + sTypeID + "' ";
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                ds = dbHelper.ExecuteDataSet(cmd);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    flag = 0;
                    strMessage = "此类别下还关联有物料，不能删除";
                }
                else
                {
                    flag = 1;

                }
            }
            catch
            {
                flag = -1;
                strMessage = "判断子类别是否能删除方法异常跳出";
            }
            return flag;
        }

        /// <summary>
        /// 获取新的父类别ID 例如：B01
        /// </summary>
        /// <returns>""获取失败，非""获取成功</returns>
        public string GetNewParentTypeID(out string strMessage)
        {
            string res = "";
            strMessage = "";
            try
            {
                strSQL = "select top 1 TypeID from TMaterialType where ParentTypeID ='0' order by TypeID desc";
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                ds = dbHelper.ExecuteDataSet(cmd);
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    res = "B01";

                }
                else
                {
                    //获取到的当前最大值，然后+1 后处理
                    res = "B" + (Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString().Substring(1, 2)) + 1).ToString().PadLeft(2, '0');

                }
            }
            catch
            {
                res = "";
                strMessage = "获取父类别编码方法异常跳出";
            }
            return res;
        }

        /// <summary>
        /// 获取当前父类下的子类ID编码 例如：B01001
        /// </summary>
        /// <param name="ParentTypeID">上一级类ID编码</param>
        /// <param name="strMessage"></param>
        /// <returns>""获取失败，非""获取成功</returns>
        public string GetNewSubTypeID(string ParentTypeID, out string strMessage)
        {
            string res = "";
            strMessage = "";
            try
            {
                strSQL = "select top 1 TypeID from TMaterialType where ParentTypeID ='" + ParentTypeID + "' order by TypeID desc";
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                ds = dbHelper.ExecuteDataSet(cmd);
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    res = ParentTypeID + "001";

                }
                else
                {
                    //获取到的当前最大值，然后+1 后处理
                    res = ParentTypeID + (Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString().Substring(3, 3)) + 1).ToString().PadLeft(3, '0');

                }
            }
            catch
            {
                res = "";
                strMessage = "获取子类别编码方法异常跳出";
            }
            return res;
        }


        /// <summary>
        /// 根据子类ID 获取子类下 新的物料编码 例如：B010010001
        /// </summary>
        /// <param name="SubTypeID"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        public string GetNewMaterialID(string SubTypeID, out string strMessage)
        {
            string res = "";
            strMessage = "";
            try
            {
                strSQL = "select top 1 Mat_Code from TBase_Material where MiniTypeID = '" + SubTypeID + "' order by Mat_Code desc";
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                ds = dbHelper.ExecuteDataSet(cmd);
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    res = SubTypeID + "0001";

                }
                else
                {
                    //获取到的当前最大值，然后+1 后处理
                    res = SubTypeID + (Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString().Substring(6, 4)) + 1).ToString().PadLeft(4, '0');

                }
            }
            catch
            {
                res = "";
                strMessage = "获取新物料编码方法异常跳出";
            }
            return res;
        }
        /// <summary>
        /// 插入物料类别表
        /// </summary>
        /// <param name="sTypeID"></param>
        /// <param name="sTypeName"></param>
        /// <param name="ParentTypeID"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1：失败 1：成功</returns>
        public int InsertMaterialType(string sTypeID, string sTypeName, string sParentTypeID, out string strMessage)
        {
            int res = 1;
            strMessage = "";
            try
            {
                strSQL = "insert into TMaterialType(TypeID, TypeName, ParentTypeID) values('" + sTypeID + "','" + sTypeName + "','" + sParentTypeID + "') ";
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                i = dbHelper.ExecuteNonQuery(cmd);
                res = i;
            }
            catch
            {
                res = -1;
                strMessage = "写入物料类别表异常跳出";
            }
            return res;
        }

        /// <summary>
        /// 更新物料类别信息 根据主键TypeID
        /// </summary>
        /// <param name="sTypeID"></param>
        /// <param name="sTypeName"></param>
        /// <param name="sParentTypeID"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1：失败 1：成功</returns>
        public int UpdateMaterialType(string sTypeID, string sTypeName, string sParentTypeID, out string strMessage)
        {
            int res = 1;
            strMessage = "";
            try
            {

                strSQL = "update TMaterialType set TypeName='" + sTypeName + "',ParentTypeID='" + sParentTypeID + "'  where TypeID='" + sTypeID + "'";
                DbCommand cmd = dbHelper.GetSqlStringCommond(strSQL);
                i = dbHelper.ExecuteNonQuery(cmd);
                res = i;
            }
            catch
            {
                res = -1;
                strMessage = "更新物料类别表异常跳出";
            }
            return res;
        }

        /// <summary>
        /// 获取父类别的数据集
        /// </summary>
        /// <returns></returns>
        public DataSet ListParentType()
        {
            try
            {
                strSQL = "select TypeID,TypeName,ParentTypeID,CID,CDT from TMaterialType where ParentTypeID='0' order by TypeID";
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                ds = dbHelper.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return ds;
        }

        /// <summary>
        /// 获取指定父类 下的子类别信息集合
        /// </summary>
        /// <param name="TypeID"></param>
        /// <returns></returns>
        public DataSet ListSubType(string ParentTypeID)
        {
            try
            {
                strSQL = "select * from TMaterialType where ParentTypeID ='" + ParentTypeID + "' order by TypeID ";
                cmd = dbHelper.GetSqlStringCommond(strSQL);
                ds = dbHelper.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return ds;
        }



    }
}
